﻿using System;
using System.Collections.Generic;
using System.Linq;
using appDataContracts;
using appInterfaces;
using System.ServiceModel;

namespace appServices
{
    public class Client : appInterfaces.IClient
    {
        private Database db = Database.getInstance();

        public List<SerializedOrder> getOrders(bool done, int userId)
        {
            List<SerializedOrder> listOrders = new List<SerializedOrder>();

            var selectOrders = db.check(db.getModel().ORDER.
                Where(ord => ord.ORDERPAID == done && ord.CLIENTID == userId));

            foreach (ORDER order in selectOrders)
            {
                listOrders.Add(Library.getSerializedOrder(order));
            }

            return listOrders;
        }

        public SerializedClient getAccoutInfo(int idUser)
        {
            try
            {
                CLIENT client = (CLIENT)db.check(db.getModel().CLIENT.
                    First(cli => cli.CLIENTID == idUser));

                return Library.getSerializedClient(client);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public bool registerAccount(SerializedClient client)
        {
            db.getModel().AddToCLIENT(new CLIENT()
            {
                CLIENTLASTNAME = client.CLIENTLASTNAME,
                CLIENTFIRSTNAME = client.CLIENTFIRSTNAME,
                CLIENTADRESS = client.CLIENTADRESS,
                CLIENTZIP = client.CLIENTZIP,
                CLIENTCITY = client.CLIENTCITY,
                CLIENTMAIL = client.CLIENTMAIL,
                CLIENTPHONE = client.CLIENTPHONE,
                CLIENTPASSWORD = client.CLIENTPASSWORD,
                CLIENTCOUNTRY = client.CLIENTCOUNTRY
            });

            try
            {
                db.getModel().SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

        public SerializedClient checkAccount(String email, String password)
        {
            try
            {
                CLIENT selectClient = (CLIENT)db.check(db.getModel().CLIENT.
                First(cli => cli.CLIENTMAIL.Equals(email) && cli.CLIENTPASSWORD.Equals(password)));

                return Library.getSerializedClient(selectClient);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public List<SerializedCatalog> getSupplierCatalogs(int supplierId)
        {
            List<SerializedCatalog> listCatalogs = new List<SerializedCatalog>();
            var selectCatalogs = db.check(db.getModel().CATALOG.
                Where(cat => cat.SUPPLIERID == supplierId));

            foreach (CATALOG catalog in selectCatalogs)
            {
                listCatalogs.Add(Library.getSerializedCatalog(catalog));
            }

            return listCatalogs;
        }

        public List<SerializedProduct> getProducts(int idCatalog)
        {
            CATALOG catalog = (CATALOG)db.check(db.getModel().CATALOG.
                First(cat => cat.CATALOGID == idCatalog));

            List<SerializedProduct> listProducts = new List<SerializedProduct>();

            foreach (PRODUCT product in catalog.PRODUCT)
            {
                listProducts.Add(Library.getSerializedProduct(product));
            }

            return listProducts;
        }

        public SerializedOrder getOrderDetails(int idOrder)
        {
            try
            {
                ORDER order = (ORDER)db.check(db.getModel().ORDER.
                    First(ord => ord.ORDERID == idOrder));

                return Library.getSerializedOrder(order);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public List<SerializedSupplier> getSuppliers()
        {
            List<SerializedSupplier> listSuppliers = new List<SerializedSupplier>();
            var selectSuppliers = db.check(db.getModel().SUPPLIER);

            foreach (SUPPLIER supplier in selectSuppliers)
            {
                listSuppliers.Add(Library.getSerializedSupplier(supplier));
            }

            return listSuppliers;
        }

        public bool updateAccountInfo(SerializedClient client)
        {
            try
            {
                CLIENT clientUpdate = (CLIENT)db.check(db.getModel().CLIENT.
                First(cli => cli.CLIENTID == client.CLIENTID));

                clientUpdate.CLIENTFIRSTNAME = client.CLIENTFIRSTNAME;
                clientUpdate.CLIENTLASTNAME = client.CLIENTLASTNAME;
                clientUpdate.CLIENTADRESS = client.CLIENTADRESS;
                clientUpdate.CLIENTPHONE = client.CLIENTPHONE;
                clientUpdate.CLIENTCITY = client.CLIENTCITY;
                clientUpdate.CLIENTPHONE = client.CLIENTPHONE;
                clientUpdate.CLIENTMAIL = client.CLIENTMAIL;
                clientUpdate.CLIENTCOUNTRY = client.CLIENTCOUNTRY;
                clientUpdate.CLIENTPASSWORD = client.CLIENTPASSWORD;

                db.getModel().AddToCLIENT(clientUpdate);
                db.getModel().SaveChanges();

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

        public SerializedProduct getProductInfo(int productId)
        {
            try
            {
                PRODUCT product = (PRODUCT)db.check(db.getModel().PRODUCT.
                First(prod => prod.PRODUCTID == productId));

                return Library.getSerializedProduct(product);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public SerializedSupplier getSupplier(int supplierId)
        {
            try
            {
                SUPPLIER supplier = (SUPPLIER)db.check(db.getModel().SUPPLIER.
                First(sup => sup.SUPPLIERID == supplierId));

                return Library.getSerializedSupplier(supplier);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public List<SerializedCatalog> getCatalogs()
        {
            List<SerializedCatalog> listCatalogs = new List<SerializedCatalog>();
            var selectCatalogs = db.check(db.getModel().CATALOG);

            foreach (CATALOG catalog in selectCatalogs)
            {
                listCatalogs.Add(Library.getSerializedCatalog(catalog));
            }

            return listCatalogs;
        }

        public SerializedCatalog getCatalog(int catalogId)
        {
            return Library.getSerializedCatalog(
                (CATALOG)db.check(db.getModel().CATALOG.First(cat => cat.CATALOGID == catalogId)));
        }

        public int addOrder(SerializedClient client, SerializedOrder order)
        {
            try
            {
                //IBank bank = new ChannelFactory<appInterfaces.IBank>("wsHttpBinding").CreateChannel();

                //if (bank.acceptTransaction())
                //{
                    ORDER newOrder = new ORDER();
                    newOrder.ORDERID = order.ORDERID;
                    newOrder.CLIENTID = order.CLIENTID;
                    newOrder.ORDERDATE = order.ORDERDATE;
                    newOrder.ORDERPAID = order.ORDERPAID;
                    newOrder.ORDERTOTAL = order.ORDERTOTAL;
                    newOrder.ORDERCARDOWNER = order.ORDERCARDOWNER;
                    newOrder.ORDERCARDNUMBER = order.ORDERCARDNUMBER;
                    newOrder.ORDERCARDEXPIRATIONDATE = order.ORDERCARDEXPIRATIONDATE;
                    newOrder.ORDERCARDSECURENUMBER = order.ORDERCARDSECURENUMBER;
                    newOrder.ORDERCARDTYPE = order.ORDERCARDTYPE;

                    db.getModel().AddToORDER(newOrder);
                    db.getModel().SaveChanges();

                    string articles= string.Empty;
                    foreach (SerializedContain article in order.ARTICLE)
                    {
                        PRODUCT product = (PRODUCT)db.check(db.getModel().PRODUCT.
                            First(prod => prod.PRODUCTID == client.CLIENTID));

                        PRODUCT myproduct = (PRODUCT)db.getModel().PRODUCT.First(p => p.PRODUCTID == article.PRODUCTID);
                        articles += article.CONTAINQUANTITY + " x " + myproduct.PRODUCTNAME + "<br/>";

                        CONTAIN contain = new CONTAIN()
                        {
                            ORDERID = newOrder.ORDERID,
                            PRODUCTID = article.PRODUCTID,
                            CONTAINSENT = article.CONTAINSENT,
                            CONTAINQUANTITY = article.CONTAINQUANTITY
                        };

                        db.getModel().AddToCONTAIN(contain);
                        db.getModel().SaveChanges();
                    }

                    Mailing mail = new Mailing(client.CLIENTMAIL, "contact@idistrict.com", "IDistrict - Votre commande a été effectué",
                        "Bonjour " + client.CLIENTLASTNAME + " " + client.CLIENTFIRSTNAME + " ! <br/><br/>" +
                        "Votre commande a été bien enregistré sous le numéro 00000" + newOrder.ORDERID + "</br></br>" +
                        "<i>" + articles + "</i><br/>" +
                        "Pour un total de <strong>" + newOrder.ORDERTOTAL + "€ </strong><br/>" +
                        "Merci de votre confiance<br/><br/> L'Equipe IDistrict!");

                    mail.send();

                    db.getModel().SaveChanges();

                    return newOrder.ORDERID;
                //}
                //else return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return 0;
            }
        }

        public SerializedSupplier getSupplierByCatalog(int catalogId)
        {
            CATALOG catalog = (CATALOG)db.check(db.getModel().CATALOG.
                First(cat => cat.CATALOGID == catalogId));

            return getSupplier(catalog.SUPPLIERID);
        }
    }
}